import { getDeleteLogApi } from "@/api/financialManagement/receivable/additionalExpense";
import { Ref } from "vue";

export default function OperationHook(state: { loading: Ref<boolean>; searchForm: AdditionalExpense.DeleteLogSearchForm; tableData: Ref<Array<AdditionalExpense.DeleteLogLisRows>> }) {
  const { searchForm, tableData, loading } = state;
  const getData = async () => {
    const params: Parameters<typeof getDeleteLogApi>[0] = {
      ...getParams()
    };
    loading.value = true;
    tableData.value = [];
    return getDeleteLogApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const getParams = () => {
    const { waybillNums, customerName, costName, createBy, pageNum, pageSize, beforePageNum } = searchForm;
    const params: any = {
      customerName,
      costName,
      createBy,
      beforePageNum,
      pageNum,
      pageSize
    };
    if (waybillNums)
      params.waybillNums = waybillNums
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    return params;
  };
  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  return {
    getData,
    searchHandle
  };
}
